<template>
  <div class="app-container">
    <div style="margin-bottom: 10px;font-size: 10px;">
<!--     选择：{{baseMap.hosname}} / {{depname}} / {{workDate}}-->
   </div>
   <el-container style="height: 100%">
     <el-aside width="350px" style="1border: 1px silver solid">
       <!-- 部门 -->
       <el-tree
         :data="departmentList"
         :props="defaultProps"
         :default-expand-all="false"
         @node-click="handleNodeClick"
         >
       </el-tree>
     </el-aside>
     <el-main style="padding: 0 0 0 20px;">
       <el-row style="width: 100%">
       <!-- 排班日期 分页 -->
         <el-tag v-for="(item,index) in bookingScheduleList" :key="item.id" @click="selectDate(item.workDate, index)" :type="index === activeIndex ? '' : 'info'" style="height: 60px;margin-right:15px;cursor:pointer;">
          {{ item.workDate }} {{ item.dayOfWeek }}<br/>
          {{ item.availableNumber }} / {{ item.reservedNumber }}
          </el-tag>
          <!-- 分页 -->
          <el-pagination
            :current-page="current"
            :total="total"
            :page-size="limit"
            class="pagination"
            layout="prev, pager, next"
            @current-change="getPage"
            v-if="bookingScheduleList.length !== 0">
          </el-pagination>
       </el-row>
       <el-row style="margin-top: 20px;">
       <!-- 排班日期对应的排班医生 -->
         <el-table
            v-loading="listLoading"
            :data="scheduleList"
            border
            fit
            highlight-current-row
            v-if="scheduleList.length !== 0">
            <el-table-column
              label="序号"
              width="60"
              align="center">
              <template slot-scope="scope">
                {{ scope.$index + 1 }}
              </template>
           </el-table-column>
           <el-table-column label="职称" width="150">
             <template slot-scope="scope">
              {{ scope.row.title }} | {{ scope.row.docname }}
             </template>
           </el-table-column>
           <el-table-column label="号源时间" width="80">
             <template slot-scope="scope">
              {{ scope.row.workTime === 0 ? "上午" : "下午" }}
             </template>
           </el-table-column>
           <el-table-column prop="reservedNumber" label="可预约数" width="80"/>
           <el-table-column prop="availableNumber" label="剩余预约数" width="100"/>
           <el-table-column prop="amount" label="挂号费(元)" width="90"/>
           <el-table-column prop="skill" label="擅长技能"/>
         </el-table>
       </el-row>
     </el-main>
   </el-container>
   </div>
</template>

<script>
import hospital from '../../api/hospital'
export default {
  name: 'schedule',
  data() {
    return {
      departmentList: [],// 部门数据
      defaultProps: {
        children: 'children',
        label: 'depname'
      },
      hoscode: '',// 医院编号
      bookingScheduleList: [],// 排班信息列表
      activeIndex: 0,// 被选中的数据的下标
      depcode: '',// 部门编号
      depname: '',// 部门名称
      workDate: null,// 工作日期
      baseMap: {},

      current: 1, // 当前页
      limit: 7, // 每页个数
      total: 0, // 总页码

      listLoading: false,
      scheduleList: []//排班详情
    }
  },
  created() {
    if (this.$route.params && this.$route.params.hoscode) {
      // 获取传递过来的医院编号
      this.hoscode = this.$route.params.hoscode;
      // 获取当前日期
      // this.workDate = this.getCurDate();
      // 获取科室信息
      this.getDepartment();
    }
  },
  methods: {
    // 通过医院编号查询医院科室信息
    getDepartment() {
      hospital.getDepartmentByHoscode(this.hoscode).then(
        response => {
          // 科室列表赋值
          this.departmentList = response.data;

          if (this.departmentList.length > 0) {
            // 默认选中第一个科室进行排班数据查询
            this.depcode = this.departmentList[0].children[0].depcode
            this.depname = this.departmentList[0].children[0].depname
            // 直接查询第一个科室对应的排班信息
            this.getPage()
          }
        }
      )
    },

    // 当节点被点击后，进行分页查询
    handleNodeClick(data) {
      // 如果点击了科室大类，就直接返回
      if (data.children != null) {
        return
      }
      // 为科室编号和科室名称赋值
      this.depcode = data.depcode;
      this.depname = data.depname;
      // 清空排班信息和排班详情
      this.bookingScheduleList = [];
      this.scheduleList = [];
      // 分页操作
      this.getPage(1);
    },

    // 分页查询方法
    getPage(page = 1) {
      this.current = page;
      this.workDate = null;
      this.activeIndex = 0;
      this.getScheduleRule();
    },

    // 获取排班规则
    getScheduleRule() {
      hospital.getScheduleRule(this.current, this.limit, this.hoscode, this.depcode).then(
        response => {
          this.bookingScheduleList = response.data.bookingScheduleRuleVoList;
          this.total = response.data.total
          // this.scheduleList = response.data.scheduleList
          this.baseMap = response.data.baseMap

          // 分页后workDate=null，默认选中第一个
          if (this.workDate == null) {
            this.workDate = this.bookingScheduleList[0].workDate
          }

          // 查询排班详情
          this.getDetailSchedule();
        }
      )
    },

    // 选择日期时触发
    selectDate(workDate, index) {
      this.workDate = workDate;
      this.activeIndex = index;
      this.listLoading = true;
      this.getDetailSchedule();
    },

    // 查询排班详情
    getDetailSchedule() {
      hospital.getDetailSchedule(this.hoscode, this.depcode, this.workDate).then(
        response => {
          this.scheduleList = response.data;
          this.listLoading = false;
        }
      )
    }

    // 获取当前日期
    /*getCurDate() {
      var datetime = new Date()
      var year = datetime.getFullYear()
      var month = datetime.getMonth() + 1 < 10 ? '0' + (datetime.getMonth() + 1) : datetime.getMonth() + 1
      var date = datetime.getDate() < 10 ? '0' + datetime.getDate() : datetime.getDate()
      return year + '-' + month + '-' + date
    }*/
  }
}
</script>

<style scoped>
  .el-tree-node.is-current > .el-tree-node__content {
     background-color: #409EFF !important;
     color: white;
   }

  .el-checkbox__input.is-checked+.el-checkbox__label {
    color: black;
   }
</style>
